package com.qidian.mespro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.mespro.mapper.*;
import com.qidian.mespro.service.ProductionOrderService;
import com.qidian.mespro.vo.ProductionOrderVO;
import com.qidianit.mes.pojo.Customer;
import com.qidianit.mes.pojo.Material;

import com.qidianit.mes.pojo.Result;


import com.qidianit.mes.pojo.pro.ProductionOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ Description:
 * @ Author: DuYaTao
 * @ Date: 2025/9/22 - 16:07
 */
@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {
    @Autowired
    private ProductionOrderMapper productionOrderMapper;
    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public Result<PageInfo<ProductionOrder>> selectAll(ProductionOrderVO productionOrder, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        // 执行了PageHelper.startPage(pageNum,pageSize);的下一条SQL就会被分页
        List<ProductionOrder> productionOrders = productionOrderMapper.selectAllList(productionOrder);
        for (ProductionOrder order : productionOrders) {
            order.setStatusDictData(dictDataMapper.selectAllByIdSysDictData(order.getStatus()));
            order.setTypeDictData(dictDataMapper.selectAllByIdSysDictData(order.getProductionOrderType()));
            order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
            Material material = materialMapper.selectByCode(order.getMaterialCode());
            material.setUnit(unitMapper.selectById(material.getUnitId()));
            order.setMaterial(material);
            if (order.getCustomerCode() != null && !order.getCustomerCode().equals("")) {
                Customer customer = customerMapper.selectByCode(order.getCustomerCode());
                order.setCustomer(customer);
            }

        }
        return Result.success(new PageInfo<>(productionOrders));
    }

    @Override
    public Result<ProductionOrder> selectById(Integer id) {
        ProductionOrder order = productionOrderMapper.selectAllByIdProductionOrder(id);
        order.setStatusDictData(dictDataMapper.selectAllByIdSysDictData(order.getStatus()));
        order.setTypeDictData(dictDataMapper.selectAllByIdSysDictData(order.getProductionOrderType()));
        order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));
        Material material = materialMapper.selectByCode(order.getMaterialCode());
        material.setUnit(unitMapper.selectById(material.getUnitId()));
        order.setMaterial(material);
        return Result.success(order);
    }

    @Override
    public Result insertAll(ProductionOrder productionOrder) {
        return productionOrderMapper.addAllProductionOrder(productionOrder)==1? Result.success():Result.error("新增工单失败");
    }
}
